﻿using System;
using System.Collections;
using System.Data;
using System.Security.Principal;
using System.Web;
using System.Reflection;
using BEGDC.PreContract.BLL;
using BEGDC.PreContract.DAL;
namespace BEGDC.PreContract.BLL.Authentication
{
	/// <summary>
	/// PCPrincipal
	/// </summary>
	public class PCPrincipal : IPrincipal
	{
        private PCIdentity indentity;

		private Hashtable inroles;
        
		/// <summary>
		/// construction function
		/// </summary>
        /// <param name="winPrincipal">PCPrincipal object</param>
        public PCPrincipal(IPrincipal winPrincipal)
		{
			indentity = new  PCIdentity(winPrincipal.Identity);
			this.LoadRoles();
		}

		#region IPrincipal Members

		/// <summary>
		/// Identity object
		/// </summary>
		public IIdentity Identity
		{
			get
			{
				return this.indentity;
			}
		}

        private string _dataPermission = " 1=2";
        public string DataPermission
        {
            get
            {
                return _dataPermission;
            }
        }
		
		/// <summary>
		/// Check user is in role group
		/// </summary>
		/// <param name="role">role list</param>
		/// <returns>true/false</returns>
		public bool IsInRole(string role)
		{
			//if (this.inroles == null)
			//	this.LoadRoles();
			string[] roles1 = role.Split('&');
			string[] roles2 = role.Split('|');
			if ((roles1.Length == 1) && (roles2.Length == 1))
			{
				return inroles.ContainsKey(role);
			}
			if (roles1.Length > 1)
			{
				foreach(string r in roles1)
				{
					if (!inroles.ContainsKey(r))
						return false;
				}
				return true;
			}
			if (roles2.Length > 1)
			{
				foreach(string r in roles2)
				{
					if (inroles.ContainsKey(r))
						return true;
				}
				return false;
			}
			return false;
		}

		#endregion

		/// <summary>
		/// reload roles and identity
		/// </summary>
		public void Reload()
		{
			this.LoadRoles();
			this.indentity.Reload();
		}

		private void LoadRoles()
		{
            BEGDC.PreContract.BLL.IUserRole userrole = (BEGDC.PreContract.BLL.IUserRole)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_UserRole_Service];

            DataTable roles = userrole.GetRoleListByEmployeeId(this.indentity.EmployeeId);

            inroles = new Hashtable();

            if (roles != null)
            {
                for (int i = 0; i < roles.Rows.Count;i++ )
                {

                    inroles.Add(roles.Rows[i][BEGDC.PreContract.DAL.Role.CONST_RoleName].ToString().Trim(), 0);
                }
            }

            BEGDC.PreContract.BLL.IEmployee emp = (BEGDC.PreContract.BLL.IEmployee) BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_Employee_Service];

            _dataPermission = emp.GetDataPermission(this.indentity.EmployeeId);
		}

		private static Hashtable all_Users = new Hashtable();

		/// <summary>
		/// Get prinicpal
		/// </summary>
		/// <param name="winPrincipal">principal</param>
        /// <returns>PCPrincipal object</returns>
		public static PCPrincipal GetPrincipal(IPrincipal winPrincipal)
		{
            lock (typeof(PCPrincipal))
			{
				string username = winPrincipal.Identity.Name;

                if (all_Users.ContainsKey(username))
				{
                    PCPrincipal re_prin = (PCPrincipal)all_Users[username];
					if (re_prin.Identity.IsAuthenticated)
						return re_prin;
					else
                        all_Users.Remove(username);
				}

                PCPrincipal principal = new PCPrincipal(winPrincipal);

                all_Users.Add(username, principal);

				return principal;
			}
		}

		/// <summary>
		/// Remove principal by adname
		/// </summary>
		/// <param name="adName">adname</param>
        public static void RemovePrincipal(string username)
		{
            lock (typeof(PCPrincipal))
			{
                if (all_Users.ContainsKey(username))
                    all_Users.Remove(username);
			}
		}
	}
}
